package com.gy.smart.home;

import android.content.Intent;
import android.net.Uri;

import com.google.gson.reflect.TypeToken;
import com.gy.smart.R;
import com.gy.smart.base.BaseActivity;
import com.gy.smart.base.BaseBean;
import com.gy.smart.handle.view.SearchView;
import com.gy.smart.home.adapter.HelpHomeAdapter;
import com.gy.smart.home.bean.HelpHomeBean;
import com.gy.smart.util.AnimUtil;
import com.gy.smart.util.ListViewItemListener;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.StringConstants;
import com.gy.smart.util.Urls;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 17/1/5
 * 说明：黄页
 */
public class HelpActivity extends BaseActivity implements SearchView.SearchViewListener,ListViewItemListener{

    private SearchView searchView;
    private PullToRefreshListView listView,listViewSearch;
    private HelpHomeAdapter adapter;
    private List<HelpHomeBean> mList,normalList,searchList,requestList;
    private int page = 1;
    private boolean isSearch = false;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_help_layout);
        setTitleMsg("黄页");
        searchView = findView(R.id.activity_help_searchview);
        listView = findView(R.id.activity_help_listview);
        listViewSearch = findView(R.id.activity_help_search_listview);
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        normalList = new ArrayList<>();
        searchList = new ArrayList<>();
        requestList = new ArrayList<>();
        adapter = new HelpHomeAdapter(this,mList);
        adapter.setListener(this);
    }

    @Override
    protected void setListener() {
        super.setListener();
        listView.setMode(PullToRefreshBase.Mode.DISABLED);
        listView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if(requestList.size() == 10 && !isLoading){
                    isLoading = true;
                    page++;
                    getList();
                }
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
        showLoadDialog(1);
        getList();
    }

    @Override
    public void onSearch(String text) {
        searchView.hideKeyboard();
    }

    @Override
    public void onClear() {

    }

    /** 获取黄页列表*/
    private void getList(){
        RequestParams params = new RequestParams(Urls.url_help_list);
        params.addParameter("page",page+"");
        params.addParameter("pagenum", StringConstants.PAGENUM);
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                Type type = new TypeToken<List<HelpHomeBean>>(){}.getType();
                requestList = gson.fromJson(result.data,type);
                mList.addAll(requestList);
                normalList.addAll(requestList);
                adapter.notifyDataSetChanged();
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }

    @Override
    public void doPassActionListener(Object obj, int org1, int org2, String str) {
        switch (org1){
            case 0:
                Intent intent = new Intent(mContext,HelpMapActivity.class);
                if(isSearch){
                    intent.putExtra("endLat",Double.valueOf(searchList.get(org2).latitude));
                    intent.putExtra("endLong",Double.valueOf(searchList.get(org2).longitude));
                    intent.putExtra("address",searchList.get(org2).address+"");
                    intent.putExtra("title",searchList.get(org2).mechanism_name+"");
                } else {
                    intent.putExtra("endLat",Double.valueOf(normalList.get(org2).latitude));
                    intent.putExtra("endLong",Double.valueOf(normalList.get(org2).longitude));
                    intent.putExtra("address",normalList.get(org2).address+"");
                    intent.putExtra("title",normalList.get(org2).mechanism_name+"");
                }
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(this);
                break;
            case 1:
                String phone = "";
                if(isSearch){
                    phone = searchList.get(org2).telephone;
                } else {
                    phone = normalList.get(org2).telephone;
                }
                Intent intent10 = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                startActivity(intent10);
                break;
        }
    }
}
